<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of urlread2</title>
  <meta name="keywords" content="urlread2">
  <meta name="description" content="URLREAD Returns the contents of a URL as a string.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../index.html">Home</a> &gt;  <a href="index.html">code</a> &gt; urlread2.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for code&nbsp;<img alt=">" border="0" src="../right.png"></a></td></tr></table>-->

<h1>urlread2
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>URLREAD Returns the contents of a URL as a string.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function [string,status] = urlread2(urlChar,method,params,timeout); </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment">URLREAD Returns the contents of a URL as a string.
   S = URLREAD('URL') reads the content at a URL into a string, S.  If the
   server returns binary data, the string will contain garbage.

   S = URLREAD('URL','method',PARAMS) passes information to the server as
   part of the request.  The 'method' can be 'get', or 'post' and PARAMS is a 
   cell array of param/value pairs.

   [S,STATUS] = URLREAD(...) catches any errors and returns 1 if the file
   downloaded successfully and 0 otherwise.

   Examples:
   s = urlread('http://www.mathworks.com');
   s = urlread('ftp://ftp.mathworks.com/pub/pentium/Moler_1.txt')
   s = urlread('file:///C:\winnt\matlab.ini')
 
   From behind a firewall, use the Preferences to set your proxy server.

   See also URLWRITE.</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
</ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="readDASURL.html" class="code" title="function [xmlStruct, xml, status] = readDASURL(dasUrl,timeout)">readDASURL</a>	READDASURL attempts to read the DAS URL within the specified timeout</li></ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [string,status] = urlread2(urlChar,method,params,timeout);</a>
0002 <span class="comment">%URLREAD Returns the contents of a URL as a string.</span>
0003 <span class="comment">%   S = URLREAD('URL') reads the content at a URL into a string, S.  If the</span>
0004 <span class="comment">%   server returns binary data, the string will contain garbage.</span>
0005 <span class="comment">%</span>
0006 <span class="comment">%   S = URLREAD('URL','method',PARAMS) passes information to the server as</span>
0007 <span class="comment">%   part of the request.  The 'method' can be 'get', or 'post' and PARAMS is a</span>
0008 <span class="comment">%   cell array of param/value pairs.</span>
0009 <span class="comment">%</span>
0010 <span class="comment">%   [S,STATUS] = URLREAD(...) catches any errors and returns 1 if the file</span>
0011 <span class="comment">%   downloaded successfully and 0 otherwise.</span>
0012 <span class="comment">%</span>
0013 <span class="comment">%   Examples:</span>
0014 <span class="comment">%   s = urlread('http://www.mathworks.com');</span>
0015 <span class="comment">%   s = urlread('ftp://ftp.mathworks.com/pub/pentium/Moler_1.txt')</span>
0016 <span class="comment">%   s = urlread('file:///C:\winnt\matlab.ini')</span>
0017 <span class="comment">%</span>
0018 <span class="comment">%   From behind a firewall, use the Preferences to set your proxy server.</span>
0019 <span class="comment">%</span>
0020 <span class="comment">%   See also URLWRITE.</span>
0021 
0022 <span class="comment">%   Matthew J. Simoneau, 13-Nov-2001</span>
0023 <span class="comment">%   Copyright 1984-2004 The MathWorks, Inc.</span>
0024 <span class="comment">%   $Revision: 1.3.2.2 $ $Date: 2004/12/27 23:33:06 $</span>
0025 
0026 <span class="comment">% This function requires Java.</span>
0027 <span class="comment">% =========================================================================</span>
0028 <span class="comment">% timeout: the reqest will be stop at a time you specified.</span>
0029 <span class="comment">%</span>
0030 <span class="comment">% An enhancement of urlread and all the modifications are labeled with (*)</span>
0031 <span class="comment">% Fu-Sung Wang, 13-Sep-2005</span>
0032 <span class="comment">% =========================================================================</span>
0033 <span class="keyword">if</span> ~usejava(<span class="string">'jvm'</span>)
0034    error(<span class="string">'MATLAB:urlread:NoJvm'</span>,<span class="string">'URLREAD requires Java.'</span>);
0035 <span class="keyword">end</span>
0036 
0037 import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
0038 
0039 <span class="comment">% Be sure the proxy settings are set.</span>
0040 com.mathworks.mlwidgets.html.HTMLPrefs.setProxySettings
0041 
0042 <span class="comment">% Check number of inputs and outputs.</span>
0043 <span class="comment">%% (*)</span>
0044 error(nargchk(1,4,nargin))                    <span class="comment">% error(nargchk(1,3,nargin))</span>
0045 error(nargoutchk(0,2,nargout))
0046 
0047 <span class="comment">%% (*)</span>
0048 <span class="comment">% if (nargin &gt; 1) &amp;&amp; ~strcmpi(method,'get') &amp;&amp; ~strcmpi(method,'post')</span>
0049 <span class="comment">%     error('MATLAB:urlread:InvalidInput','Second argument must be either &quot;get&quot; or &quot;post&quot;.');</span>
0050 <span class="comment">% end</span>
0051 
0052 <span class="comment">% Do we want to throw errors or catch them?</span>
0053 <span class="keyword">if</span> nargout == 2
0054     catchErrors = true;
0055 <span class="keyword">else</span>
0056     catchErrors = false;
0057 <span class="keyword">end</span>
0058 
0059 <span class="comment">% Set default outputs.</span>
0060 string = <span class="string">''</span>;
0061 status = 0;
0062 
0063 <span class="comment">% GET method.  Tack param/value to end of URL.</span>
0064 <span class="keyword">if</span> (nargin &gt; 1) &amp;&amp; strcmpi(method,<span class="string">'get'</span>)
0065     <span class="keyword">if</span> mod(length(params),2) == 1
0066         error(<span class="string">'MATLAB:urlread:InvalidInput'</span>,<span class="string">'Invalid parameter/value pair arguments.'</span>);
0067     <span class="keyword">end</span>
0068     <span class="keyword">for</span> i=1:2:length(params)
0069         <span class="keyword">if</span> (i == 1), separator = <span class="string">'?'</span>; <span class="keyword">else</span>, separator = <span class="string">'&amp;'</span>; <span class="keyword">end</span>
0070         param = char(java.net.URLEncoder.encode(params{i}));
0071         value = char(java.net.URLEncoder.encode(params{i+1}));
0072         urlChar = [urlChar separator param <span class="string">'='</span> value];
0073     <span class="keyword">end</span>
0074 <span class="keyword">end</span>
0075 
0076 <span class="comment">% Try to use the native handler, not the ice.* classes.</span>
0077 <span class="keyword">if</span> strncmpi(<span class="string">'http:'</span>,urlChar,5)
0078     <span class="keyword">try</span>
0079         handler = sun.net.www.protocol.http.Handler;
0080     <span class="keyword">catch</span>
0081         handler = [];
0082     <span class="keyword">end</span>
0083 <span class="keyword">else</span>
0084     handler = [];
0085 <span class="keyword">end</span>
0086 
0087 <span class="comment">% Create the URL object.</span>
0088 <span class="keyword">try</span>
0089     <span class="keyword">if</span> isempty(handler)
0090         url = java.net.URL(urlChar);
0091     <span class="keyword">else</span>
0092         url = java.net.URL([],urlChar,handler);
0093     <span class="keyword">end</span>
0094 <span class="keyword">catch</span>
0095     <span class="keyword">if</span> catchErrors, <span class="keyword">return</span>
0096     <span class="keyword">else</span> error(<span class="string">'MATLAB:urlread:InvalidUrl'</span>,<span class="string">'Either this URL could not be parsed or the protocol is not supported.'</span>,catchErrors);
0097     <span class="keyword">end</span>
0098 <span class="keyword">end</span>
0099 
0100 
0101 
0102 <span class="comment">% Open a connection to the URL.</span>
0103 urlConnection = url.openConnection;
0104 
0105 <span class="comment">%% (*)</span>
0106 urlConnection.setReadTimeout(timeout);
0107 
0108 <span class="comment">% POST method.  Write param/values to server.</span>
0109 <span class="keyword">if</span> (nargin &gt; 1) &amp;&amp; strcmpi(method,<span class="string">'post'</span>)
0110     <span class="keyword">try</span>
0111         urlConnection.setDoOutput(true);
0112         urlConnection.setRequestProperty( <span class="keyword">...</span>
0113             <span class="string">'Content-Type'</span>,<span class="string">'application/x-www-form-urlencoded'</span>);
0114         printStream = java.io.PrintStream(urlConnection.getOutputStream);
0115         <span class="keyword">for</span> i=1:2:length(params)
0116             <span class="keyword">if</span> (i &gt; 1), printStream.print(<span class="string">'&amp;'</span>); <span class="keyword">end</span>
0117             param = char(java.net.URLEncoder.encode(params{i}));
0118             value = char(java.net.URLEncoder.encode(params{i+1}));
0119             printStream.print([param <span class="string">'='</span> value]);
0120         <span class="keyword">end</span>
0121         printStream.close;
0122     <span class="keyword">catch</span>
0123         <span class="keyword">if</span> catchErrors, <span class="keyword">return</span>
0124         <span class="keyword">else</span> error(<span class="string">'MATLAB:urlread:ConnectionFailed'</span>,<span class="string">'Could not POST to URL.'</span>);
0125         <span class="keyword">end</span>
0126     <span class="keyword">end</span>
0127 <span class="keyword">end</span>
0128 
0129 <span class="comment">% Read the data from the connection.</span>
0130 <span class="keyword">try</span>
0131     inputStream = urlConnection.getInputStream; 
0132     byteArrayOutputStream = java.io.ByteArrayOutputStream;
0133     <span class="comment">% This StreamCopier is unsupported and may change at any time.</span>
0134     isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
0135     isc.copyStream(inputStream,byteArrayOutputStream);
0136     inputStream.close;
0137     byteArrayOutputStream.close;
0138     string = char(byteArrayOutputStream.toByteArray');
0139 <span class="keyword">catch</span>
0140     <span class="keyword">if</span> catchErrors, <span class="keyword">return</span>
0141     <span class="keyword">else</span> error(<span class="string">'MATLAB:urlread:ConnectionFailed'</span>,<span class="string">'Error downloading URL.'</span>);
0142     <span class="keyword">end</span>
0143 <span class="keyword">end</span>
0144 
0145 status = 1;</pre></div>
<hr><address>Generated on Tue 17-Feb-2009 16:59:45 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>